package com.jblank.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.jblank.common.config.GlobalConfig;
import com.jblank.common.config.QueryConfig;
import com.jblank.common.config.SaveConfig;
import com.jblank.common.utils.GlobalUtils;
import com.jblank.dao.Dao;
import com.jblank.dto.DTO;

/**
 * Dao Oracle实现
 * 
 * @author kim
 * @since 2013-07-22
 */
@Component("daoOracle")
public class DaoOracle implements Dao {

	private Log logger = LogFactory.getLog(this.getClass());
	private JdbcTemplate jdbcTemplate;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	@Resource(name = "jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@Override
	public boolean update(String tableName, Map varMap, String primaryKey,
			String value) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean update(String tableName, Map varMap, String cond) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void callPro(String prcName, DTO prcDto) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean delete(String tableName, String condExp, Object[] condVars) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean delete(String tableName, String condExp, Map condVars) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean delete(String tableName, String primaryKey, String value) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean delete(String tableName, String cond) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean insert(String tableName,List flds,List values){
		boolean res = false;
		if(tableName != null && !tableName.isEmpty() && flds != null && !flds.isEmpty()){
			String sql = " insert into " + tableName + "(#flds#) values (#values#)";
			String fld = "";
			String value = "";
			for(int i = 0; i < flds.size(); i++){
				if(fld.length() > 0) fld += ",";
				if(value.length() > 0) value += ",";
				fld += flds.get(i);
				value += "?";
			}
			fld += "," + GlobalConfig.PrimaryKey;
			value += ",?";
			values.add(GlobalUtils.getUUID());
			System.out.println(values);
			sql = sql.replaceAll("#flds#", fld);
			sql = sql.replaceAll("#values#", value);
			res = jdbcTemplate.update(sql, values.toArray()) == 1;
		}
		return res;
	}
	
	@Override
	public boolean insert(SaveConfig saveConfig) {
		String tableName = saveConfig.getTableName();
		List flds = saveConfig.getFlds().subList(0, saveConfig.getFlds().size());
		List values = saveConfig.getValues().subList(0, saveConfig.getValues().size());
		return insert(tableName,flds,values);
	}
	@Override
	public boolean insertBatch(DTO batchDto) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public List query(QueryConfig queryConfig) {
		String tableName = queryConfig.getTableName();
		String flds = queryConfig.getFlds();
		String cond = queryConfig.getCond();
		String condExp = queryConfig.getCondExp();
		String condValues = queryConfig.getCondValues();
		String order = queryConfig.getOrder();
		int start = queryConfig.getStart();
		int limit = queryConfig.getLimit();
		if(cond != null && cond.length() > 0){
			if(limit > 0){
				return queryPage(tableName,flds,cond,order,start,limit);
			}else{
				return query(tableName,flds,cond,order);
			}
		}else{
			if(limit > 0){
				return queryPage(tableName,flds,condExp,condValues != null ? condValues.split(",") : null ,order,start,limit);
			}else{
				return query(tableName,flds,condExp,condValues);
			}
		}
	}
	@Override
	public List queryPage(String tableName, String flds, String condExp,Object[] condVars, String order, int start, int limit) {
		String aliasName = " t1";
		String sql = " select " + flds + " from ( select row_number() over(order by " + order + ") as R," + aliasName + ".* from " + 
					tableName + aliasName +
					((condExp != null && condExp.length() > 0) ? " where " + condExp : "") + " ) TempTable where TempTable.R between " + 
					(start + 1) + " and " + (start + limit);
		List result = null;
		if(condExp != null && condExp.length() > 0 && condVars == null){
			result = query(sql,condVars);		
		}else{
			result = query(sql);
		}
		return result;
	}
	@Override
	public List queryPage(String tableName, String flds, String cond,String order, int start, int limit) {
		return queryPage(tableName,flds,cond,null,order,start,limit);
	}
	@Override
	public List query(String sql, Object[] condVars) {
		return jdbcTemplate.queryForList(sql,condVars);
	}
	@Override
	public List query(String sql) {
		return jdbcTemplate.queryForList(sql);
	}
	@Override
	public List query(String tableName, String flds, String condExp,Object[] condVars,String order) {
		String sql = " select " + flds + " from " + tableName + " where " + condExp;
		System.out.println("sql:" + sql);
		return jdbcTemplate.queryForList(sql,condVars);
	}
	@Override
	public List query(String tableName, String flds, String condExp, Map condVars,String order) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List query(String tableName, String flds, String primaryKey,String value,String order) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List query(String tableName, String flds, String cond,String order) {
		String sql = " select " + flds + " from " + tableName + " where " + cond;
		System.out.println("sql:" + sql);
		return jdbcTemplate.queryForList(sql);
	}
	@Override
	public int queryCount(QueryConfig queryConfig) {
		String tableName = queryConfig.getTableName();
		String cond = queryConfig.getCond();
		String condExp = queryConfig.getCondExp();
		String condValues = queryConfig.getCondValues();//bug 需要数组
		
		if(condExp != null && condExp.length() > 0){
			return queryCount(tableName,condExp,condValues);
		}else{
			return queryCount(tableName,cond);
		}
	}
	@Override
	public int queryCount(String tableName, String condExp, Object[] condVars) {
		String sql = "select count(*) from " + tableName + " where " + condExp;
		return jdbcTemplate.queryForInt(sql, condVars);
	}
	@Override
	public int queryCount(String tableName, String condExp, Map condVars) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int queryCount(String tableName, String primaryKey, String value) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int queryCount(String tableName, String cond) {
		String sql = " select count(*) from " + tableName + ((cond!=null && cond.length() > 0) ? " where " + cond : "");
		return jdbcTemplate.queryForInt(sql);
	}
	@Override
	public boolean update(String tableName, Map varMap, String condExp,Object[] condVars) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean update(String tableName, Map varMap, String condExp,Map condVars) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public Object queryForObject(String tableName, String fldName,String condExp, Object[] condVars) {
		String sql = " select " + fldName + " from " + tableName + " where " + condExp;
		List list = jdbcTemplate.queryForList(sql, condVars);
		Iterator it = list.iterator();
		if(it.hasNext()) {
		    Map map = (Map) it.next();
		    return map.get(fldName);
		}else{
			return null;
		}
	}
	@Override
	public Object queryForObject(String tableName, String fldName,String condExp, Map condVars) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object queryForObject(String tableName, String fldName,String primaryKey, String value) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object queryForObject(String tableName, String fldName, String cond) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean delete(SaveConfig saveConfig) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean update(SaveConfig saveConfig) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public Dao newData() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
